<template>
  <div>
    <el-button type="info" round @click="dialogFormVisible = true"
      >添加用户</el-button
    >
    <el-table :data="userList" stripe style="width: 100% height:20px">
      <el-table-column prop="username" label="用户名"> </el-table-column>
      <el-table-column prop="role_name" label="昵称"> </el-table-column>
      <el-table-column prop="mobile" label="电话号"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
      <!-- 修改状态 -->
      <el-table-column prop="mg_state" label="状态">
        <template slot-scope="scope">
          <el-switch
            @change="changeState(scope.row)"
            v-model="scope.row.mg_state"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- 查找 -->
          <el-button
            icon="el-icon-search"
            circle
            size="mini"
            @click="look(scope.row)"
          ></el-button>
          <!-- 修改用户 -->
          <el-button
            type="warning"
            icon="el-icon-edit"
            circle
            size="mini"
            @click="Editstart(scope.row)"
          >
          </el-button>
          <!-- 删除用户 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            size="mini"
            @click="outusers(scope.row)"
          ></el-button>
          <!-- 分配用户角色 -->
          <el-button
            type="warning"
            icon="el-icon-star-off"
            circle
            size="mini"
            @click="fengpei(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页按钮 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[3, 4, 5, 8]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 弹出框添加 -->
    <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
      <el-form :model="postUsers">
        <el-form-item label="用户名">
          <el-input v-model="postUsers.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="postUsers.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="postUsers.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="postUsers.email" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addusers">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改用户信息弹出框 -->
    <el-dialog title="编辑用户" :visible.sync="dialogEditVisible">
      <el-form :model="stateEdit">
        <el-form-item label="用户名">
          <el-input v-model="stateEdit.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="stateEdit.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="stateEdit.email" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="putusers">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 查找用户信息 -->
    <el-dialog title="查找用户" :visible.sync="dialogFormlookedVisible">
      <el-form :model="lookinfo">
        <el-form-item label="用户名">
          <el-input v-model="lookinfo.username" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="lookinfo.mobile" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input
            v-model="lookinfo.email"
            autocomplete="off"
            :disabled="true"
          ></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 分配用户角色 -->
    <el-dialog title="分配角色" :visible.sync="dialogEditFormVisible">
      <el-form :model="stateEditForm">
        <el-form-item label="用户名">
          <el-input
            v-model="stateEditForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <!-- 获取角色信息 -->
        <el-select v-model="roleId" placeholder="请选择">
          <el-option
            v-for="item in roleList"
            :key="item.id"
            :label="item.roleName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="Formusers">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //分页的数据
      userList: [],
      total: 0,
      pageSize: 3,
      //添加用户的
      dialogFormVisible: false,
      postUsers: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      //修改用户信息的
      dialogEditVisible: false,
      stateEdit: {},
      //分配用户角色
      dialogEditFormVisible: false,
      stateEditForm: {},
      roleList: [],
      roleId: "",
      //查看用户信息
      dialogFormlookedVisible: false,
      lookinfo: {},
    };
  },

  methods: {
    // 查看用户信息
    look(userinfo) {
      this.lookinfo = userinfo;
      this.dialogFormlookedVisible = true;
    },
    // 分配用户角色
    async Formusers() {
      let { data } = await this.$req.put(
        `/users/${this.stateEditForm.id}/role`,
        { rid: this.roleId } //发送用户id和角色的id
      );
      // console.log(data);
      //不管成功失败都把弹出框给改掉，设置为false
      this.dialogEditFormVisible = false;
      if (data.meta.status == 200) {
        this.$message({
          message: data.meta.msg,
          type: "success",
        });
        // this.roleList.roleName = this.stateEditForm.
        this.getUsers(1, this.pageSize); //在第几页展示
      } else {
        this.$message.reeor(data.meta.msg);
      }
    },

    // 分配用户角色弹窗获取用户名
    async fengpei(userinfo) {
      //用户当前角色
      // this.role_name=userinfo.role_name
      this.dialogEditFormVisible = true;
      this.stateEditForm = userinfo;
      //获取用户信息并展示
      let { data } = await this.$req.get("/roles");
      this.roleList = data.data;
    },

    //删除用户
    outusers(userinfo) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        let { data } = await this.$req.delete("/users/" + userinfo.id);
        //  console.log(data);
        if (data.meta.status == 200) {
          this.$message({
            type: "success",
            message: data.meta.msg,
          });
          this.getUsers(1, this.pageSize); //在第几页展示
        } else {
          this.$message.reeor(data.meta.msg);
        }
      });
    },

    // 修改用户信息
    async putusers() {
      let { data } = await this.$req.put(
        "/users/" + this.stateEdit.id,
        this.stateEdit
      );
      if (data.meta.status == 200) {
        this.$message({
          message: data.meta.msg,
          type: "success",
        });
        this.dialogEditVisible = false;
      } else {
        this.$message.error(data, meta.msg);
      }
    },

    // 弹窗获取用户信息
    Editstart(userinfo) {
      this.dialogEditVisible = true;
      this.stateEdit = userinfo;
    },

    //修改用户状态
    async changeState(rows) {
      // console.log(rows);
      var { data } = await this.$req.put(
        `/users/${rows.id}/state/${rows.mg_state}`
      );
      console.log(data);
      if (data.meta.status == 200) {
        // this.mg_start=true
        this.$message({
          message: data.meta.msg,
          type: "success",
        });
      } else {
        //如果服务器设置失败，状态改变成原来的状态
        rows.mg_state = !rows.mg_state;
        this.$message.error(data.meta.msg);
      }
    },

    //添加用户
    async addusers() {
      console.log(this.postUsers);
      let { data } = await this.$req.post("/users", this.postUsers);
      if (data.meta.status == 201) {
        //状态正确的情况
        this.$message({
          //提醒用户的弹框
          message: data.meta.msg,
          type: "success",
        });
        this.dialogFormVisible = false; //关闭弹窗
        this.getUsers(1, this.pageSize); //在第几页展示
      } else {
        this.$message.error(data.meta.msg);
      }
    },

    //公用
    async getUsers(pagenum, pagesize) {
      var { data } = await this.$req.get(
        `/users?pagenum=${pagenum}&pagesize=${pagesize}`
      );
      if (data.meta.status == 200) {
        console.log(data.data);
        this.userList = data.data.users;
        this.total = data.data.total;
      } else {
        this.$message.error(data.meta.msg);
      }
    },

    //设置跳转到第几页的数据
    async handleCurrentChange(pagenum) {
      this.getUsers(pagenum, this.pageSize);
    },
    //设置每页的条数
    async handleSizeChange(pagesize) {
      this.getUsers(1, pagesize);
    },
  },

  //页面加载，就获取到每页的条数
  async mounted() {
    this.getUsers(1, 3);
  },
};
</script>

<style>
</style>